const TableStore = function (table, initialState = {}) {
    if (!table) {
        throw new Error('!table');
    }
    this.table = table;
    this.states = {
        _columns: [],
        originColumns: [],
        columns: [],
        leafColumns: [],
        isComplex: false,
        data: null,
    };

    // 属性拷贝
    for (let prop in initialState) {
        if (initialState.hasOwnProperty(prop) && this.states.hasOwnProperty(prop)) {
            this.states[prop] = initialState[prop];
        }
    }
};

const doFlattenColumns = (columns) => {
    const result = [];
    columns.forEach((column) => {
        result.push(column);
    });
    return result;
};
TableStore.prototype.updateColumns = function () {
    const states = this.states;
    const _columns = states._columns || [];

    const notFixedColumns = _columns.filter(column => !column.fixed);
    states.originColumns = [].concat(notFixedColumns);

    const leafColumns = doFlattenColumns(notFixedColumns);

    states.leafColumns = leafColumns.length;

    states.columns = [].concat(leafColumns);
    states.isComplex = false;
};
TableStore.prototype.mutations = {
    // table.vue watch.data 调用
    setData(states, data) {
        states._data = data;
        states.data = data;
    },
    insertColumn(states, column, index) {
        let array = states._columns;
        if (typeof index !== 'undefined') {
            // 在index的位置插入column
            array.splice(index, 0, column);
        } else {
            array.push(column);
        }
    },
};
TableStore.prototype.commit = function commit(name, ...args) {
    const mutations = this.mutations;
    if (mutations[name]) {
        mutations[name].apply(this, [this.states].concat(args));
    } else {
        throw new Error(`Action not found: ${name}`);
    }
};
export default TableStore;